package com.icss.ah.auditGoods.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.icss.ah.base.action.BaseAction;
import com.icss.ah.base.bean.EasyuiDataGridJson;
import com.icss.ah.base.bean.Json;
import com.icss.ah.login.bean.UserInfoBean;
@Controller
@Scope("prototype")
public class AuditGoodsAction  extends BaseAction {
	private final Logger errlog = Logger.getLogger("E");
	private final Logger infolog = Logger.getLogger("I");
	/**
	 * 进入审核列表
	 * @return
	 */
	public String toAuditGoodsList(){
		return "toAuditGoodsList";
	}
	/**
	 * 查询审核列表
	 */
	public void queryAuditGoods(){
		request = this.getRequest();
		String result = "";
		try {
			String title = request.getParameter("title");
			title = title == null ? "" : title;
			String frmStartTime = request.getParameter("frmStartTime");
			frmStartTime = frmStartTime == null ? "" : frmStartTime;
			String frmEndTime = request.getParameter("frmEndTime");
			frmEndTime = frmEndTime == null ? "" : frmEndTime;
			String auditStatus = request.getParameter("auditStatus");
			auditStatus = auditStatus == null ? "" : auditStatus;
			String userCode=getUserCodeFromSession();
			UserInfoBean userInfo=getUserInfo(userCode);
			String shopId=userInfo.getShopId();   	//获取店铺id 
			String corpId = userInfo.getCorpId();	//公众号id
			
			dataGridJson = new EasyuiDataGridJson();
			HashMap map = new HashMap();
			map.put("shopId",shopId);
			map.put("corpId",corpId);
			map.put("title", title);
			map.put("frmStartTime", frmStartTime);
			map.put("frmEndTime", frmEndTime);
			map.put("auditStatus", auditStatus);
			
			map.put("startNo",Integer.valueOf(getStartIndexMysql()));
			map.put("pageRows",Integer.valueOf(getEndIndexMysql()));
			List<Map> auditGoodsList = new ArrayList();
			auditGoodsList = auditGoodsService.getAuditGoodsList(map);	//查询列表
			long auditGoodsCount = auditGoodsService.getAuditGoodsCount(map);//查询总记录数
			dataGridJson.setRows(auditGoodsList);
			dataGridJson.setTotal(auditGoodsCount);
			result = JSONObject.fromObject(dataGridJson).toString();
			infolog.info("queryAuditGoods|查询审核列表成功");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("queryAuditGoods|查询审核列表失败：",e);
		} finally {
			str2Resp(result);
		}
	}
	/**
	 * 进入审核界面
	 * @return
	 */
	public String toAuditGoodPage(){
		request = this.getRequest();
		return "toAuditGoodPage";
	}
	/**
	 * 修改审核状态
	 */
	public void  auditIfBatch(){
		jsonText = new Json();
		request = this.getRequest();
		String  uuid =request.getParameter("uuid");
		uuid = uuid == null ? "" :uuid;
		String  rejectReason =request.getParameter("rejectReason");//审核不通过时，填写审核原因
		rejectReason = rejectReason == null ? "" :rejectReason;
		String  refundType =request.getParameter("refundType");	//审核通过1，不通过2
		refundType = refundType == null ? "" :refundType;
		String  ifBatch =request.getParameter("ifBatch");
		ifBatch = ifBatch == null ? "" :ifBatch;
		
//		String userCode=getUserCodeFromSession();
//		UserInfoBean userInfo=getUserInfo(userCode);
//		String shopId=userInfo.getShopId();
		HashMap map  = new HashMap();
//		map.put("shopId", shopId);
		map.put("rejectReason",rejectReason);
		map.put("refundType",refundType);
		String restr = "";
		if(ifBatch.equals("batch")){			//批量审核
			String[] ids = uuid.split(",");
			for(int i=0;i<ids.length;i++){
				if(!"".equals(ids[i])){
					map.put("auditId", ids[i]);
					restr = auditGoodsService.auditChecked(map);
				}
			}
		}else if(ifBatch.equals("noBatch")){	//单个商品审核
			map.put("auditId", uuid);
			restr = auditGoodsService.auditChecked(map);
		}
		if(restr.equals("1")){
			jsonText.setSuccess(true);
		} else {
			jsonText.setSuccess(false);
		}
		str2Resp(JSONObject.fromObject(jsonText).toString());
	}
}
